package com.hitqz.robot.biz.schedule;

import com.hitqz.robot.api.business.enums.RobotOtherCommandEnum;
import com.hitqz.robot.api.common.entity.Robot;
import com.hitqz.robot.biz.service.RobotService;
import com.hitqz.robot.common.core.util.R;
import com.hitqz.robot.driver.parent.RobotDriverService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * 顶升状态
 * @author xupkun
 * @date 2025/4/17
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class JackStatusSchedule {

    private final RobotService robotService;

    private final RobotDriverService robotDriverService;


    private Integer lastJackStatus = null;

    private final RestTemplate restTemplate;

    //@Scheduled(fixedRate = 1000L)
    //public void sendStatus() {
    //    R result = robotDriverService.executeOtherCommand(RobotOtherCommandEnum.JACKING_STATUS,null);
    //    int  jackStatus = (int) result.getData();
    //    boolean hasSendStatus = false;
    //    int sendStatus = 1;
    //    if (lastJackStatus == null) {
    //        // 提交当前状态
    //        lastJackStatus = jackStatus;
    //    }
    //    if (lastJackStatus != jackStatus) {
    //        hasSendStatus = true;
    //        if (jackStatus == 0) {
    //            // 上升中
    //            sendStatus = 6;
    //        }
    //        if (jackStatus == 1) {
    //            // 顶部
    //            sendStatus = 2;
    //        }
    //        if (jackStatus == 2) {
    //            // 下降中
    //            sendStatus = 7;
    //        }
    //        if (jackStatus == 3) {
    //            // 底部
    //            sendStatus = 1;
    //        }
    //    }
    //    if (hasSendStatus) {
    //        lastJackStatus = jackStatus;
    //        Robot robot = robotService.getDefaultRobot();
    //        String uri = "http://192.10.13.20:9999/sb/upSBZT?sbbh=" + robot.getRobotSn() + "&sbstatus=" + sendStatus;
    //        String sendStatusResult = restTemplate.postForObject(uri, "", String.class);
    //        log.info("发送顶升状态: {}---{}", uri, sendStatusResult);
    //    }
    //
    //}
//    @Scheduled(fixedRate = 500L)
    public void sendStatus() {
        try {
            R result = robotDriverService.executeOtherCommand(RobotOtherCommandEnum.JACKING_STATUS, null);
            Object data = result.getData();
            if (!(data instanceof Integer)) {
                log.warn("获取顶升状态失败，返回数据不是Integer类型");
                return;
            }
            int jackStatus = (Integer) data;

            boolean hasSendStatus = false;
            int sendStatus = 1;

            if (lastJackStatus == null) {
                // 初始状态也需要上报
                hasSendStatus = true;
                lastJackStatus = jackStatus;
            }

            if (!lastJackStatus.equals(jackStatus)) {
                hasSendStatus = true;
                switch (jackStatus) {
                    case 0:
                        sendStatus = 6; // 上升中
                        break;
                    case 1:
                        sendStatus = 2; // 顶部
                        if (robotDriverService.getWebPos().getTaskStatus() == 2) {
                            sendStatus = 3;
                        }
                        break;
                    case 2:
                        sendStatus = 7; // 下降中
                        break;
                    case 3:
                        sendStatus = 1; // 底部
                        break;
                    default:
                        log.warn("未知的顶升状态码: {}", jackStatus);
                        return;
                }
            }

            if (hasSendStatus) {
                lastJackStatus = jackStatus;
                Robot robot = robotService.getDefaultRobot();
                String uri = "http://192.10.13.20:9999/sb/upSBZT?sbbh=" + robot.getRobotSn() + "&sbstatus=" + sendStatus;
                try {
                    String sendStatusResult = restTemplate.postForObject(uri, "", String.class);
                    log.info("发送顶升状态: {}---{}", uri, sendStatusResult);
                    if (sendStatusResult == null || !sendStatusResult.contains("success")) {
                        log.warn("发送顶升状态失败，响应内容: {}", sendStatusResult);
                    }
                } catch (Exception e) {
                    log.error("发送顶升状态时发生异常", e);
                }
            }
        } catch (Exception e) {
            log.error("定时任务 sendStatus 执行异常", e);
        }
    }


}
